package com.way.Java多线程编程核心技术v2.chapter02_对象及变量的并发访问._2_2_synchronized同步语句块.验证方法被调用是随机的;

import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: my-projects
 * @DESCRIPTION:
 * @USER: 魏彪
 * @DATE: 2023/11/29 15:18
 */
public class MyList {

    private List<String> list = new ArrayList<>();

    synchronized public void add(String username) {
        System.out.println("ThreadName=" + Thread.currentThread().getName()
                + "执行了add方法！");
        list.add(username);
        System.out.println("ThreadName=" + Thread.currentThread().getName()
                + "退出了add方法！");
    }

    synchronized public int getSize() {
        System.out.println("ThreadName=" + Thread.currentThread().getName()
                + "执行了getSize方法！");
        int sizeValue = list.size();
        System.out.println("ThreadName=" + Thread.currentThread().getName()
                + "退出了getSize方法！");
        return sizeValue;
    }
}
